package com.zib.bilibili.view.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zib.bilibili.R;
import com.zib.bilibili.adapter.LiveFragmentAdapter;
import com.zib.bilibili.base.BaseMvpFragment;
import com.zib.bilibili.bean.LiveBean;
import com.zib.bilibili.presenter.LivePresenter;
import com.zib.bilibili.utils.ImageLoader;
import com.zib.bilibili.view.ILiveView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/6.
 */
public class LiveFragment extends BaseMvpFragment<ILiveView, LivePresenter> implements ILiveView, XRecyclerView.LoadingListener {
    private static final String TAG = "LiveFragment";
    HeadViewHolder hvh;

    @Bind(R.id.recyclerView_fragment_live)
    XRecyclerView recyclerViewFragmentLive;

    private LiveFragmentAdapter liveAdapter;

    public static LiveFragment newInstance() {
        LiveFragment fragment = new LiveFragment();
        return fragment;
    }

    @Override
    protected LivePresenter initPresenter() {
        return new LivePresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_live;
    }

    @Override
    public void initView() {
        //初始化布局管理器并为RecyclerView设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewFragmentLive.setLayoutManager(layoutManager);

        //设置刷新的样式
        recyclerViewFragmentLive.setRefreshProgressStyle(ProgressStyle.SquareSpin);
        hvh = new HeadViewHolder();
        //设置适配器
        liveAdapter = new LiveFragmentAdapter(context, R.layout.item_recyclerview_live);
        recyclerViewFragmentLive.setAdapter(liveAdapter);
        //rcv添加头部View
        recyclerViewFragmentLive.addHeaderView(hvh.headView);
        recyclerViewFragmentLive.setRefreshing(true);
        recyclerViewFragmentLive.setLoadingListener(this);
        //蛇者不下拉刷新
        recyclerViewFragmentLive.noMoreLoading();
        //首次加载数据
        presenter.getLiveData();
    }

    class HeadViewHolder {
        public View headView;
        @Bind(R.id.find_head_banner_covb)
        ConvenientBanner covb;
        @Bind(R.id.live_phone)
        RelativeLayout livePhone;
        @Bind(R.id.live_paint)
        RelativeLayout livePaint;
        @Bind(R.id.live_zhai)
        RelativeLayout liveZhai;
        @Bind(R.id.live_game)
        RelativeLayout liveGame;
        @Bind(R.id.live_danji)
        RelativeLayout liveDanji;
        @Bind(R.id.live_bisai)
        RelativeLayout liveBisai;
        @Bind(R.id.live_classify)
        RelativeLayout liveClassify;
        @Bind(R.id.live_all)
        RelativeLayout liveAll;
        @Bind(R.id.live_guanzhu)
        RelativeLayout liveGuanzhu;
        @Bind(R.id.live_center)
        RelativeLayout liveCenter;
        @Bind(R.id.live_search)
        RelativeLayout liveSearch;


        public HeadViewHolder() {
            this.headView = inflater.inflate(R.layout.recyclerview_live_header, recyclerViewFragmentLive, false);
            ButterKnife.bind(this, headView);
        }


        private void initHeader(LiveBean liveBean) {
            initBannerView(liveBean);
            livePhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "手机直播", Toast.LENGTH_SHORT).show();
                }
            });
            livePaint.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "绘画专区", Toast.LENGTH_SHORT).show();
                }
            });
            liveZhai.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "宅萌推荐", Toast.LENGTH_SHORT).show();
                }
            });
            liveGame.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "网络游戏", Toast.LENGTH_SHORT).show();
                }
            });
            liveDanji.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "单机联机", Toast.LENGTH_SHORT).show();
                }
            });
            liveBisai.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "电子竞技", Toast.LENGTH_SHORT).show();
                }
            });
            liveClassify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "全部分类", Toast.LENGTH_SHORT).show();
                }
            });
            liveAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "全部直播", Toast.LENGTH_SHORT).show();
                }
            });
            liveGuanzhu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "关注主播", Toast.LENGTH_SHORT).show();
                }
            });
            liveCenter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "直播中心", Toast.LENGTH_SHORT).show();
                }
            });
            liveSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "搜索直播", Toast.LENGTH_SHORT).show();
                }
            });
        }

        private void initBannerView(LiveBean liveBean) {
            covb.setPages(new BannerCBViewHolderCreator(), liveBean.getData().getBanner())
                    .setPageIndicator(new int[]{R.mipmap.dot_light, R.mipmap.dot_dark})
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_LEFT);
        }

        private class BannerCBViewHolderCreator implements CBViewHolderCreator<ImageHolder> {
            @Override
            public ImageHolder createHolder() {
                return new ImageHolder();
            }
        }

        public class ImageHolder implements Holder<LiveBean.DataBean.BannerBean> {

            private ImageView img;

            @Override
            public View createView(Context context) {
                img = new ImageView(context);
                return img;
            }

            @Override
            public void UpdateUI(Context context, int position, LiveBean.DataBean.BannerBean data) {
                ImageLoader.getInstance().displayImage(getActivity(), data.getImg(), img);
            }


        }
    }


    @Override
    public void onSuccess(LiveBean liveBean) {
        liveAdapter.addAll(liveBean.getData().getPartitions());
        hvh.initHeader(liveBean);
        //这里改了
        if (recyclerViewFragmentLive != null) {
            recyclerViewFragmentLive.refreshComplete();
        }
    }

    @Override
    public void onFaile() {

    }

    @Override
    public void onRefresh() {
        Log.i(TAG, "---onRefresh: ");
        presenter.getLiveData();
    }

    @Override
    public void onLoadMore() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
